package client;



import java.util.ArrayList;
import java.util.Random;
import SensorFrameWork.SensorWebService;
import SensorFrameWork.SensorWebServiceService;

import saxion.sensor.structuur.Sensor;


public class Sensors {
	
	
	private SensorWebService SensorFramework;
	private SensorCreator sensorCreator;

	private  ArrayList<Sensor> sensors;
	private int aantalSensors = 10;
	private int loops = 10;
	

	
	   public static void main(String[] args) throws Exception {
		   
	     Sensors Sensors = new Sensors();
	     Sensors.sendRandomData();
	        
	    }
	   
	   /**
	    * 
	    */
	   public Sensors()
	   {
  
		   init();
		   sensors = new ArrayList<Sensor>();
		   
		   createSensors();
	   }
	   
	   /**
	    * 
	    */
	   public void init(){
		   
		   SensorWebServiceService service = new SensorWebServiceService();
		   SensorFramework = service.getSensorWebServicePort();
		   
		   sensorCreator= new SensorCreator();
		   
	   }
	   
	   
	   
	   /**
	    * 
	    */
	   public void createSensors()
	   {
		   Random rand = new Random(); 
		   
		   for(int i=1; i < aantalSensors; i++ ){  
			   
			   	Sensor sensor = sensorCreator.createRandomSensor(i,rand.nextInt(10)+2 );
			   	
			   	byte[] data = sensorCreator.convertSensorToXML(sensor);
				int sensorId = SensorFramework.registerSensor(data);
				
				if(sensorId == -1){
					 System.err.println("sensor register ERROR!");
				}
				else
				{
					sensor.setId(sensorId);
					sensors.add(sensor);
				}
				   
		   }

	   }
	   
	   /**
	    * 
	    * @throws Exception
	    */
	   public void sendRandomData() throws Exception
	   {
		  for(int i =0; i < loops; i++){
			  
		  
		   for(Sensor sensor: sensors)
		   {
			   byte[] data = sensorCreator.generateRandomDataForSensor(sensor);
			   boolean status = SensorFramework.sendSensorData(data);
				 
		        System.out.println("Data send : " + status);
		   }
		  }
		   
	   }

}
